// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import Character from "../BASE/Character";
import ComponentBase from "../BASE/ComponentBase";
import Qin from "../BASE/qin";
import realThing from "../BASE/realThing";
import Cspine from "../BASE/spineANDgif/Cspine";
import { gif } from "../BASE/spineANDgif/gif";
import AnimalManager from "../Manager/AnimalManager";
import turn from "../game/time/turn";

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends ComponentBase {
dorn:string="day"
k:cc.Node=null;
AM:cc.Node=null;

 SKYrn: number[] = [];
// 序列数字
SKYs: number = 0;
onLoad(): void {
   // this.water2.timeArray=[63,63,63,63,63,63,63,63,63,63]
turn.Roo.push(this)
this.k=cc.find("Canvas/thingsManager/nightL")
this.AM= cc.find("Canvas/DituManager/New Node/AnimalManager")
    //this.changeDirection(6);
 //设置动画结束监听
 this.node.children[0].children[0].getComponent(sp.Skeleton).setCompleteListener((trackEntry:sp.spine.TrackEntry) => {
//清空监听
// this.spine2.setCompleteListener(null);
//console.log(trackEntry.animation.name+"动画播放完毕！");
this.getRj()

});


}


start() {
    // 初始化随机数组
    this.generateRandomArray();
}

// 生成随机数组的方法
private generateRandomArray(): void {
    this.SKYrn = [];
    const onesAndTwosCount = Math.floor(Math.random() * 3) + 3; // 3到5个1或2
    for (let i = 0; i < onesAndTwosCount; i++) {
        this.SKYrn.push(Math.random() > 0.5 ? 1 : 2);
    }
    // 确保至少有一个1和一个2
    if (this.SKYrn.filter(num => num === 1).length === 0) {
        this.SKYrn.push(1);
    }
    if (this.SKYrn.filter(num => num === 2).length === 0) {
        this.SKYrn.push(2);
    }
    // 其余填充0
    while (this.SKYrn.length < 20) {
        this.SKYrn.push(0);
    }
    // 打乱数组顺序
    for (let i = this.SKYrn.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [this.SKYrn[i], this.SKYrn[j]] = [this.SKYrn[j], this.SKYrn[i]];
    }
    return
}

// 触发方法
public getRj(): void {
    switch (this.SKYrn[this.SKYs]) {
        case 0:
            this.methodForZero();
            break;
        case 1:
            this.methodForOne();
            break;
        case 2:
            this.methodForTwo();
            break;
        default:
            this.SKYs = 0; // 重置序列数字
            break;
    }
    this.SKYs++;
    if ( this.SKYs==20) {
        this.SKYs=0
    }
}

// 为0触发的方法
private methodForZero(): void {
    if (turn.DorN=="day") {
        this.node.children[0].children[0].getComponent(sp.Skeleton).animation="day"
    }else{ this.node.children[0].children[0].getComponent(sp.Skeleton).animation="night"}
}

// 为1触发的方法
private methodForOne(): void {
    if (turn.DorN=="day") {
        this.node.children[0].children[0].getComponent(sp.Skeleton).animation="day1"
    }else{ this.node.children[0].children[0].getComponent(sp.Skeleton).animation="night1"}
}

// 为2触发的方法
private methodForTwo(): void {
    if (turn.DorN=="day") {
        this.node.children[0].children[0].getComponent(sp.Skeleton).animation="day2"
    }else{ this.node.children[0].children[0].getComponent(sp.Skeleton).animation="night2"}
}


roundPass(): void {
    if (turn.DorN!=this.dorn) {
        this.dorn=turn.DorN
        if( this.dorn=="day"){this.node.getComponent(cc.Animation).play("today");
        this.node.children[0].children[0].getComponent(sp.Skeleton).animation="change to day"
    for(let a of cc.find("Canvas/DituManager/New Node/AnimalManager").getComponent(AnimalManager).YuanGong){a.node.color=cc.color(200,190,239)}
    cc.tween(this.k).to(3, { opacity:0}).start();
    


    for(let a of cc.find("Canvas/DituManager/New Node/AnimalManager").children){
        a.color=cc.color(255,255,255)
    
        for(let c of a.children){c.color=cc.color(255,255,255)}    
    
    }

}


       
    
    
    
    
    
    
    
    
    
    else{this.node.getComponent(cc.Animation).play("tonight");
      

        for(let a of cc.find("Canvas/DituManager/New Node/AnimalManager").children){

if (a.getComponent(Qin)==null) {
    a.color=cc.color(200,190,239)

    if (a.getComponent(Character)==null) {
        for(let c of a.children){c.color=cc.color(200,190,239)}    
    } else{
         for(let c of a.children){if (c.getComponent(sp.Skeleton)!=null) {
            
            
            c.color=cc.color(200,190,239)}   
        
        
    }}
    }
}


      
    
        cc.tween(this.k).to(3, { opacity:255}).start();
        this.node.children[0].children[0].getComponent(sp.Skeleton).animation="change to night"}
    }

let a= this.node.children[0].children[1]
cc.tween(a).to(0.3, { rotation: a.rotation+30}).start();
}

}
